package com.raising.sc.upload.file.client;

import com.alibaba.fastjson.JSON;
import com.raising.sc.upload.file.client.entity.FileEntity;
import com.raising.sc.core.util.common.FileConvertUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 上传流数据
 * @author GaoYuan
 * @date 2018/4/10 上午11:30
 */
public class UploadWithStreamClient {

    public static void main(String[] args){
        UploadWithStreamClient.upload("http://127.0.0.1:10009/file/upload", FileConvertUtil.fileToInputStream(new File("/Users/gy/Downloads/cxyy.png")),"mmm/12/cxyy.png");
    }

    /**
     * 上传流数据
     * @author GaoYuan
     * @date 2018/4/10 上午11:51
     * @param url 上传接口
     * @param inputStream 文件字符流
     * @param filename 文件名称可以是 单纯的文件名称 比如 name.png
     *                 也可以是带目录结构的文件 比如 floder1/floder2/cxyy.png
     *       DateFormatUtils.format(new Date(), "yyyyMMdd")
     *       UUID.randomUUID()
     */
    public static FileEntity upload(String url, InputStream inputStream, String filename) {
        //创建HttpClient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //构建POST请求   请求地址请更换为自己的。
        //1)
        HttpPost post = new HttpPost(url);
        try {
            //文件路径请换成自己的
            //2)
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            //第一个参数为 相当于 Form表单提交的file框的name值
            //第二个参数就是我们要发送的InputStream对象了
            //第三个参数是文件名
            //3)
            builder.addBinaryBody("uploadfile", inputStream, ContentType.create("multipart/form-data"), filename);
            //4)构建请求参数 普通表单项
            builder.addPart("id",new StringBody("12",ContentType.MULTIPART_FORM_DATA));
            HttpEntity entity = builder.build();
            post.setEntity(entity);

            //发送请求
            HttpResponse response = client.execute(post);
            entity = response.getEntity();
            String result = EntityUtils.toString(entity, "UTF-8");
            FileEntity fileEntity=  JSON.parseObject(result,FileEntity.class);
            return fileEntity;
        }catch (IOException e){
            return null;
        }finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
            if(client!=null){
                try{
                    client.close();
                }catch (Exception e){
                }
            }
        }
    }
}
